/*
 * Copyright (c) 2012 SUBSECRETARIA DEFENSORIA DEL ESPACIO PUBLICO - SDEP.
 * All Rights Reserved.
 * This software is the confidential and proprietary information of SDEP
 * Developed By CONSTULT-SOFT S.A, Development Team 2012.
 *
 */
package co.gov.medellin.sdep.sisdep.domain.gestion;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import co.gov.medellin.sdep.sisdep.gestion.exception.ArticuloAreaResponsableException;
import co.gov.medellin.sdep.sisdep.gestion.exception.ArticuloAreaResponsableRuntimeException;
import co.gov.medellin.sdep.sisdep.gestion.service.IArticuloAreaResponsableService;
import co.gov.medellin.sdep.sisdep.common.AppConstants;
import co.gov.medellin.sdep.sisdep.domain.BaseController;
import co.gov.medellin.sdep.sisdep.domain.MessageModalWindowController;
import co.gov.medellin.sdep.sisdep.model.ArticuloAreaResponsable;

/**
 * @author Usuario
 * 
 */
public class ArticuloAreaResponsableController extends BaseController implements
		Serializable {

	/**
	 * generated serial version
	 */
	private static final long serialVersionUID = -3285110767384990403L;

	/**
	 * Constructor por defecto, inicializa los elemento basicos del Controlador
	 */
	public ArticuloAreaResponsableController() {
		cleanSearchValues();
		cleanValues();
	}

	/** bean para el manejo del formulario de busqueda ArticuloAreaResponsable */
	private ArticuloAreaResponsable articuloAreaResponsableSearch = new ArticuloAreaResponsable();

	/** Objeto contenedor del listado de ArticuloAreaResponsable luego de una consulta. */
	private DataModel articuloAreaResponsableListModel;

	/** bean para el manejo del formulario de creacion/edicion. ArticuloAreaResponsable */
	private ArticuloAreaResponsable articuloAreaResponsable = new ArticuloAreaResponsable();

	/** instancia de servicio articuloAreaResponsableService */
	private IArticuloAreaResponsableService articuloAreaResponsableService;

	// --------------------------------------------------------------------
	// --- metodos para atender acciones init()
	// --------------------------------------------------------------------

	/**
	 * 
	 * @return String
	 */
	public String searchArticuloAreaResponsableByKey() {
		String msg = "";
		try {
			List valores = null;
			valores = getArticuloAreaResponsableService().load(1, 10,
					this.articuloAreaResponsableSearch);
			this.articuloAreaResponsableListModel.setWrappedData(valores);
		} catch (Exception e) {
			msg = e.getMessage();
			e.printStackTrace(System.err);
		}
		if (this.articuloAreaResponsableListModel.getWrappedData() == null) {
			if(msg.equals("")){
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		} else {
			if (((List) this.articuloAreaResponsableListModel.getWrappedData()).isEmpty()) {
				msg = AppConstants.NO_RECORDS_FOUND;
			}
		}
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage(msg, null, null);
		return AppConstants.ARTICULO_AREA_RESPONSABLE_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String create() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.articuloAreaResponsable.getId().longValue() == 0L) {
				getArticuloAreaResponsableService().insert(this.articuloAreaResponsable);
				msg = AppConstants.SUCCESS_CREATE;
			}
		} catch (ArticuloAreaResponsableException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticuloAreaResponsableRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Art\u00EDculo \u00C1rea Responsable " + msg, null, null);
		return AppConstants.ARTICULO_AREA_RESPONSABLE_SEARCH_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String update() {
		String msg = AppConstants.DEFAULT_ERROR;
		try {
			if (this.articuloAreaResponsable.getId().longValue() != 0L) {
				getArticuloAreaResponsableService().update(this.articuloAreaResponsable);
				msg = AppConstants.SUCCESS_UPDATE;
			}
		} catch (ArticuloAreaResponsableException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		} catch (ArticuloAreaResponsableRuntimeException e) {
			msg = e.getEndUserMessage();
			e.printStackTrace(System.err);
		}
		cleanValues();
		((MessageModalWindowController) getBean("messageController"))
				.setSuccessMessage("- Art\u00EDculo \u00C1rea Responsable " + msg, null, null);
		return AppConstants.ARTICULO_AREA_RESPONSABLE_SEARCH_VIEW;
	}

	// --------------------------------------------------------------------
	// --- metodos para atender acciones end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public String createArticuloAreaResponsable() {
		articuloAreaResponsable = new ArticuloAreaResponsable();
		cleanValues();
		return AppConstants.ARTICULO_AREA_RESPONSABLE_CREATE_VIEW;
	}

	/**
	 * 
	 * @return
	 */
	public String updateArticuloAreaResponsable() {
		Map params = getExternalContext().getRequestParameterMap();
		String articuloAreaResponsableDataIndex = (String) params
				.get("articuloAreaResponsableDataIndex");
		this.articuloAreaResponsable = (ArticuloAreaResponsable) ((List) this.articuloAreaResponsableListModel
				.getWrappedData())
				.get(Integer.parseInt(articuloAreaResponsableDataIndex));
		cleanSearchValues();
		return AppConstants.ARTICULO_AREA_RESPONSABLE_UPDATE_VIEW;
	}

	/**
	 *
	 * @return
	 */
	public String cancelArticuloAreaResponsable() {
		cleanSearchValues();
		return AppConstants.ARTICULO_AREA_RESPONSABLE_SEARCH_VIEW;
	}

	/**
	 *
	 * @return
	 */
	public String cancelArticuloAreaResponsableOpcion() {
		cleanValues();
		cleanSearchValues();
		return AppConstants.ARTICULO_AREA_RESPONSABLE_SEARCH_VIEW;
	}

	/**
	 * limpia el contenido de los valores que se ingresan para buscar
	 */
	public final void cleanSearchValues(){
						  articuloAreaResponsableSearch.setAreaResponsable("");
						  articuloAreaResponsableSearch.setIdentificacion("");
						  articuloAreaResponsableSearch.setPrimerApellido("");
						  articuloAreaResponsableSearch.setEstado("");
	}

	/**
	 * limpia el contenido de los valores que se ingresan (Create, Update)
	 */
	public final void cleanValues(){
						articuloAreaResponsable.setId(new Long(0L));
						  articuloAreaResponsable.setAreaResponsable("");
						  articuloAreaResponsable.setIdentificacion("");
						articuloAreaResponsable.setIdTipoIdentificacion(new Long(0L));
						  articuloAreaResponsable.setPrimerNombre("");
						  articuloAreaResponsable.setSegundoNombre("");
						  articuloAreaResponsable.setPrimerApellido("");
						  articuloAreaResponsable.setSegundoApellido("");
						  articuloAreaResponsable.setObservacion("");
						  articuloAreaResponsable.setEstado("");
		this.articuloAreaResponsableListModel = new ListDataModel();
	}

	// --------------------------------------------------------------------
	// --- metodos para atender reglas de navegacion end()
	// --------------------------------------------------------------------

	// --------------------------------------------------------------------
	// --- getters and setters init()
	// --------------------------------------------------------------------

	/**
	 * 
	 */
	public ArticuloAreaResponsable getArticuloAreaResponsableSearch() {
		return articuloAreaResponsableSearch;
	}

	/**
	 * 
	 * @param articuloAreaResponsableSearch
	 */
	public void setarticuloAreaResponsableSearch(ArticuloAreaResponsable articuloAreaResponsableSearch) {
		this.articuloAreaResponsableSearch = articuloAreaResponsableSearch;
	}

	/**
	 * 
	 * @return
	 */
	public DataModel getarticuloAreaResponsableListModel() {
		return articuloAreaResponsableListModel;
	}

	/**
	 * 
	 * @param articuloAreaResponsableListModel
	 */
	public void setarticuloAreaResponsableListModel(DataModel articuloAreaResponsableListModel) {
		this.articuloAreaResponsableListModel = articuloAreaResponsableListModel;
	}

	/**
	 * 
	 * @return
	 */
	public ArticuloAreaResponsable getArticuloAreaResponsable() {
		return articuloAreaResponsable;
	}

	/**
	 * 
	 * @param articuloAreaResponsable
	 */
	public void setarticuloAreaResponsable(ArticuloAreaResponsable articuloAreaResponsable) {
		this.articuloAreaResponsable = articuloAreaResponsable;
	}

	/**
	 * obtencion del servicio articuloAreaResponsableService
	 * 
	 * @return
	 */
	public IArticuloAreaResponsableService getArticuloAreaResponsableService() {
		return articuloAreaResponsableService;
	}

	/**
	 * establecimiento del servicio articuloAreaResponsableService, la inyeccion de la
	 * dependencia la cubre SpringFramework
	 * 
	 * @param articuloAreaResponsableService
	 */
	public void setArticuloAreaResponsableService(
			IArticuloAreaResponsableService articuloAreaResponsableService) {
		this.articuloAreaResponsableService = articuloAreaResponsableService;
	}

	// --------------------------------------------------------------------
	// --- getters and setters end()
	// --------------------------------------------------------------------

}
